package utils.security;

import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;

public final class DigestUtils {

    public static void main(String[] args) {
        String source = "Enum";
        byte[] sha256Bytes = sha256(source.getBytes());
        String s = Hex.toHexString(sha256Bytes);//返回64个字符，一个字符对应原始的四位，总共
    }

    public static byte[] sha256(byte[] sourceBytes) {
        SHA256Digest sha256Digest = new SHA256Digest();
        sha256Digest.update(sourceBytes, 0, sourceBytes.length);
        byte[] sha256Bytes = new byte[sha256Digest.getDigestSize()];
        sha256Digest.doFinal(sha256Bytes, 0);
        return sha256Bytes;
    }

    public static byte[] sm3(byte[] sourceBytes) {
        SM3Digest sm3Digest = new SM3Digest();
        sm3Digest.update(sourceBytes, 0, sourceBytes.length);
        byte[] sm3Bytes = new byte[sm3Digest.getDigestSize()];
        sm3Digest.doFinal(sm3Bytes, 0);
        return sm3Bytes;
    }
}

